<template>
  <div class="data-integration">
    <!-- Header -->
    <div class="page-header">
      <h1>数据集成</h1>
      <div class="header-actions">
        <el-button :icon="Refresh" circle @click="refreshData" />
        <el-button :icon="Setting" circle />
      </div>
    </div>

    <!-- Data Source Status -->
    <el-card class="section-card">
      <template #header>
        <div class="card-header">
          <span>数据源状态</span>
        </div>
      </template>
      <div class="data-sources">
        <el-card
          v-for="source in dataSources"
          :key="source.id"
          :class="['source-card', source.status]"
          shadow="hover"
        >
          <div class="source-info">
            <el-icon><component :is="source.icon" /></el-icon>
            <div class="source-details">
              <div class="source-name">{{ source.name }}</div>
              <div class="source-sync">最后同步: {{ source.lastSync }}</div>
            </div>
          </div>
          <el-tag
            :type="source.status === 'normal' ? 'success' : 'warning'"
            size="small"
          >
            {{ source.status === 'normal' ? '正常' : '延迟' }}
          </el-tag>
        </el-card>
      </div>
    </el-card>

    <!-- Storage Status -->
    <el-card class="section-card">
      <template #header>
        <div class="card-header">
          <span>数据存储状态</span>
        </div>
      </template>
      <div class="storage-status">
        <div class="usage-bar">
          <div class="usage-info">
            <span>存储使用率</span>
            <span>{{ storageStatus.usage }}%</span>
          </div>
          <el-progress
            :percentage="storageStatus.usage"
            :color="storageColor"
          />
        </div>
        <div class="storage-details">
          <el-card shadow="hover">
            <div class="storage-item">
              <div class="label">实时数据</div>
              <div class="value">{{ storageStatus.realtimeData }}GB</div>
            </div>
          </el-card>
          <el-card shadow="hover">
            <div class="storage-item">
              <div class="label">历史数据</div>
              <div class="value">{{ storageStatus.historicalData }}GB</div>
            </div>
          </el-card>
        </div>
      </div>
    </el-card>

    <!-- Backup Status -->
    <el-card class="section-card">
      <template #header>
        <div class="card-header">
          <span>数据备份状态</span>
        </div>
      </template>
      <div class="backup-status">
        <el-card shadow="hover">
          <div class="backup-item">
            <el-icon><Upload /></el-icon>
            <div class="backup-info">
              <div class="backup-name">自动备份</div>
              <div class="backup-schedule">{{ backupStatus.schedule }}</div>
            </div>
            <el-switch
              v-model="backupStatus.autoBackup"
              @change="handleBackupToggle"
            />
          </div>
        </el-card>
        <el-card shadow="hover">
          <div class="backup-item">
            <el-icon><Timer /></el-icon>
            <div class="backup-info">
              <div class="backup-name">上次备份</div>
              <div class="backup-time">{{ backupStatus.lastBackup }}</div>
            </div>
            <el-button type="primary" size="small">查看详情</el-button>
          </div>
        </el-card>
      </div>
    </el-card>

    <!-- Security Status -->
    <el-card class="section-card">
      <template #header>
        <div class="card-header">
          <span>数据安全</span>
        </div>
      </template>
      <div class="security-status">
        <el-card shadow="hover">
          <div class="security-item">
            <el-icon><Lock /></el-icon>
            <span>数据加密</span>
            <el-tag type="success" size="small">已启用</el-tag>
          </div>
        </el-card>
        <el-card shadow="hover">
          <div class="security-item">
            <el-icon><User /></el-icon>
            <span>访问控制</span>
            <el-tag type="success" size="small">已启用</el-tag>
          </div>
        </el-card>
        <el-card shadow="hover">
          <div class="security-item">
            <el-icon><Document /></el-icon>
            <span>操作日志</span>
            <el-button type="primary" size="small">查看日志</el-button>
          </div>
        </el-card>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useDataIntegrationStore } from '@/stores/dataIntegration'
import { ElMessage } from 'element-plus'
import {
  Refresh,
  Setting,
  Upload,
  Timer,
  Lock,
  User,
  Document
} from '@element-plus/icons-vue'

const store = useDataIntegrationStore()

// 计算属性
const storageColor = computed(() => {
  const usage = store.storageStatus.usage
  if (usage < 70) return '#67C23A'
  if (usage < 90) return '#E6A23C'
  return '#F56C6C'
})

// 方法
const refreshData = async () => {
  try {
    await Promise.all([
      store.fetchDataSources(),
      store.fetchStorageStatus(),
      store.fetchBackupStatus(),
      store.fetchSecurityStatus()
    ])
    ElMessage.success('数据已更新')
  } catch (error) {
    ElMessage.error('数据更新失败')
  }
}

const handleBackupToggle = async (value) => {
  try {
    await store.toggleAutoBackup(value)
    ElMessage.success('备份设置已更新')
  } catch (error) {
    ElMessage.error('备份设置更新失败')
  }
}

// 生命周期钩子
onMounted(() => {
  refreshData()
})
</script>

<style lang="scss" scoped>
.data-integration {
  padding: 16px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  h1 {
    font-size: 20px;
    font-weight: 600;
    color: #303133;
  }
}

.section-card {
  margin-bottom: 16px;

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.data-sources {
  display: flex;
  flex-direction: column;
  gap: 12px;

  .source-card {
    &.normal {
      border-left: 4px solid #67C23A;
    }

    &.warning {
      border-left: 4px solid #E6A23C;
    }

    .source-info {
      display: flex;
      align-items: center;
      gap: 12px;

      .el-icon {
        font-size: 24px;
        color: #409EFF;
      }

      .source-details {
        .source-name {
          font-weight: 500;
          color: #303133;
        }

        .source-sync {
          font-size: 12px;
          color: #909399;
        }
      }
    }
  }
}

.storage-status {
  .usage-bar {
    margin-bottom: 16px;

    .usage-info {
      display: flex;
      justify-content: space-between;
      margin-bottom: 8px;
      color: #606266;
    }
  }

  .storage-details {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;

    .storage-item {
      text-align: center;

      .label {
        color: #909399;
        font-size: 14px;
      }

      .value {
        font-size: 20px;
        font-weight: 600;
        color: #303133;
        margin-top: 4px;
      }
    }
  }
}

.backup-status {
  display: flex;
  flex-direction: column;
  gap: 12px;

  .backup-item {
    display: flex;
    align-items: center;
    gap: 12px;

    .el-icon {
      font-size: 24px;
      color: #409EFF;
    }

    .backup-info {
      flex: 1;

      .backup-name {
        font-weight: 500;
        color: #303133;
      }

      .backup-schedule,
      .backup-time {
        font-size: 12px;
        color: #909399;
      }
    }
  }
}

.security-status {
  display: flex;
  flex-direction: column;
  gap: 12px;

  .security-item {
    display: flex;
    align-items: center;
    gap: 12px;

    .el-icon {
      font-size: 20px;
      color: #409EFF;
    }

    span {
      flex: 1;
      color: #303133;
    }
  }
}
</style> 